﻿using System;
using System.IO;
using System.Web.Mvc;
using smp.ButterflyCakes.Models;
using smp.ButterflyCakes.Models.FormModels.Attachments;
using smp.ButterflyCakes.Repository.Implementation;
using smp.ButterflyCakes.Repository.Interfaces;
using smp.ButterflyCakes.Web.Infrastructure;

namespace smp.ButterflyCakes.Web.Controllers
{
    public class AttachmentController : BaseController
    {
        private readonly IAttachmentRepository _orderRepository;
        
        public AttachmentController()
            :base(new Log(), new Cache())
        {
            _orderRepository =  new AttachmentRespository();
        }

        public AttachmentController(ILog log, ICache cache, IAttachmentRepository repository)
            : base(log, cache)
        {
            _orderRepository = repository;
        }

        [HttpGet]
        [GuidId]
        public virtual ActionResult AddFiles(Guid id)
        {
            var fileData = _orderRepository.GetFilesView(id, true);
            var data = new UploadFileModel { OrderId = id, ViewFiles = fileData };
            var hasUploaded = TempData["uploaded"];
            if (hasUploaded != null)
            {
                data.Uploaded = (bool)hasUploaded;
                var uploadSuccessful = TempData["uploadSuccessful"];
                data.WasSuccessful = uploadSuccessful != null && (bool)uploadSuccessful;
            }
            return View("AddFiles", data);
        }

        [HttpGet]
        [GuidId]
        public virtual ActionResult Attachments(Guid id)
        {
            var fileData = _orderRepository.GetFilesView(id, false);
            return PartialView("Attachments", fileData);
        }

        [HttpGet]
        [GuidId]
        public FileContentResult GetImage(Guid id)
        {
            var content = _orderRepository.GetImageContent(id);
            return File(content, "image/jpg");
        }

        [HttpGet]
        [GuidId]
        public FileResult DownloadFile(Guid id)
        {
            var attachment = _orderRepository.GetAttachment(id);
            if (attachment == null)
                return null;
            return File(attachment.Content, attachment.MimeType, attachment.Name);
        }

        [HttpPost]
        public virtual ActionResult UploadAFile(UploadFileModel input)
        {
            try
            {
                var attachment = new AttachementItem
                {
                    OrderId = input.OrderId,
                    Id = Guid.NewGuid(),
                    IsImage = input.IsImage,
                    Description = input.Description,
                    Content = new byte[input.File.ContentLength],
                    Extension = Path.GetExtension(input.File.FileName),
                    Name = Path.GetFileName(input.File.FileName),
                    MimeType = input.File.ContentType
                };
                input.File.InputStream.Read(attachment.Content, 0, input.File.ContentLength);
                TempData.Add("uploaded", true);
                TempData.Add("uploadSuccessful", _orderRepository.AddAttachment(input.OrderId, attachment));
                return AddFiles(input.OrderId);
            }
            catch (Exception ex)
            {
                TempData.Add("uploaded", true);
                TempData.Add("uploadSuccessful", false);
                return AddFiles(input.OrderId);
            }
        }

        [HttpGet]
        [GuidId]
        public virtual ActionResult DeleteFile(Guid id)
        {
            return AddFiles(_orderRepository.DeleteAttachment(id));
        }
    }
}
